<?php

/**
 * Library helper to create HTML Table
 * @author GusDeCooL <gusdecool@gmail.com>
 * @package com.gusdecool.phplib.table
 * @subpackage com.gusdecool.phplib.table
 * @version 1.0
 */
Class HtmlTable {

	/**
	 * Table Wrapper
	 * @var string
	 */
	private $_tableWrapOpen;

	/**
	 * Table wrap close
	 * @var string
	 */
	private $_tableWrapClose = '</table>';

	/**
	 * Contains Table Data
	 * @var array
	 */
	private $_tableRow = array();

	/**
	 * Temporary data cell
	 * @var array
	 */
	private $_dataCell = array();

	/**
	 * 
	 * @param array $attr Table Attribute
	 */
	public function __construct($attr = array()) {
		$this->setTable($attr);
	}

	/**
	 * Set table attribute
	 * @param array $attr
	 */
	public function setTable($attr) {
		$attribute = $this->_parserAttribute($attr);
		$this->_tableWrapOpen = '<table ' . $attribute . '>';
	}

	/**
	 * Add data cell
	 * @param string $data Data Cell
	 * @param array $attr Cell Attribute
	 */
	public function addDataCell($data = '', $attr = array()) {
		$attribute = $this->_parserAttribute($attr);
		$cell = '<td ' . $attribute . '>' . $data . '</td>';
		array_push($this->_dataCell, $cell);
	}

	/**
	 * Add table row
	 * @param array $attr
	 */
	public function addRow($attr = array()) {
		$attribute = $this->_parserAttribute($attr);
		$row = '<tr ' . $attribute . '>'
				. $this->_parserCell($this->_dataCell)
				. $this->_rowAlignment() . '</tr>';

		array_push($this->_tableRow, $row);
		$this->_resetDataCell();
	}

	/**
	 * Parser cell attribute
	 * @param array $attr Cell Attribute
	 * @return string
	 */
	private function _parserAttribute($attr) {
		$return = array();
		foreach ($attr as $key => $value) {
			array_push($return, $key . '="' . $value . '"' . " ");
		}

		return implode(' ', $return);
	}

	/**
	 * Return parser cell, this parser already include alignment
	 * @param array $cell
	 * @return string
	 */
	private function _parserCell($cell) {
		$return = $this->_cellAlignment()
				. implode($this->_cellAlignment(), $cell);
		return $return;
	}

	/**
	 * Parser table row
	 * @return string
	 */
	private function _parserRow() {
		return $this->_rowAlignment()
				. implode($this->_rowAlignment(), $this->_tableRow)
				. $this->_rowAlignment();
	}

	/**
	 * Return Cell Alignment
	 * @return string
	 */
	private function _cellAlignment() {
		return "\n\t\t";
	}

	/**
	 * Return Row Alignment
	 * @return string
	 */
	private function _rowAlignment() {
		return "\n\t";
	}

	private function _tableAlignment() {
		return "\n";
	}

	/**
	 * Reset data cell for reuse
	 */
	private function _resetDataCell() {
		$this->_dataCell = array();
	}

	private function _resetTableRow() {
		$this->_tableRow = array();
	}

	private function _resetTableWrap() {
		$this->_tableWrapOpen = '<table>';
	}

	/**
	 * Reset table data for reuse
	 */
	private function _reset() {
		$this->_resetTableRow();
		$this->_resetTableWrap();
	}

	/**
	 * Output HTML table
	 * @return string
	 */
	public function generate() {
		$return = $this->_tableAlignment() . $this->_tableWrapOpen
				. $this->_parserRow()
				. $this->_tableAlignment() . $this->_tableWrapClose;

		$this->_reset();
		return $return;
	}

}

?>